package com.txy.servlet;

import com.txy.constant.Constant;
import com.txy.entity.Department;
import com.txy.entity.Employee;
import com.txy.service.DepartmentService;
import com.txy.service.EmployeeService;
import com.txy.service.impl.DepartmentServiceImpl;
import com.txy.service.impl.EmployeeServiceImpl;
import com.txy.util.PageInfo;
import com.txy.vo.EmployeeParam;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2022-11-08 16:24
 * Description：<描述>
 */
@WebServlet("*.emp")
public class EmployeeServlet extends HttpServlet {

    EmployeeService employeeService = new EmployeeServiceImpl();
    DepartmentService departmentService = new DepartmentServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        if("/findAll.emp".equals(path)){
            findAll(req,resp);
        }else  if("/removeById.emp".equals(path)){
            removeById(req,resp);
        }else if("/toAdd.emp".equals(path)){
            toAdd(req,resp);
        }else if("/add.emp".equals(path)){
            add(req,resp);
        }else if("/toModify.emp".equals(path)){
            toModify(req,resp);
        }else if("/modify.emp".equals(path)){
            modify(req,resp);
        }
    }

    private void modify(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");

        Employee emp = new Employee();
        emp.setId(Integer.parseInt(req.getParameter("id")));
        emp.setName(req.getParameter("name"));
        emp.setAge(Integer.parseInt(req.getParameter("age")));
        emp.setSex(req.getParameter("sex"));
        emp.setHeight(Double.parseDouble(req.getParameter("height")));
        try {
            emp.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("birthday")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Department dept = new Department();
        dept.setId(Integer.parseInt(req.getParameter("deptId")));
        emp.setDepartment(dept);

        employeeService.modify(emp);
        resp.sendRedirect("findAll.emp");
    }

    private void toModify(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        Employee emp = employeeService.findById(id);
        req.setAttribute("emp",emp);
        req.getRequestDispatcher("/WEB-INF/pages/modify.jsp").forward(req,resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("utf-8");

        Employee emp = new Employee();
        emp.setName(req.getParameter("name"));
        emp.setAge(Integer.parseInt(req.getParameter("age")));
        emp.setSex(req.getParameter("sex"));
        emp.setHeight(Double.parseDouble(req.getParameter("height")));
        try {
            emp.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(req.getParameter("birthday")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Department dept = new Department();
        dept.setId(Integer.parseInt(req.getParameter("deptId")));
        emp.setDepartment(dept);

        employeeService.add(emp);
        resp.sendRedirect("findAll.emp");
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/add.jsp").forward(req,resp);
    }

    private void removeById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id"));
        employeeService.removeById(id);
        resp.sendRedirect("findAll.emp");
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // List<Employee> list = employeeService.findAll();
        // req.setAttribute("list",list);

        req.setCharacterEncoding("utf8");

        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String s = req.getParameter("deptId");
        int deptId = s!=null ? Integer.parseInt(s) : 0;
        EmployeeParam param = new EmployeeParam(name, sex, deptId);

        String str = req.getParameter("pageNo");
        int pageNo = str != null ? Integer.parseInt(str) : Constant.PAGENO;

        PageInfo<Employee> pageInfo = employeeService.findByPage(pageNo, Constant.PAGESIZE, param);
        req.setAttribute("pageInfo",pageInfo);

        // 查找所有部门并存储在Session中
        List<Department> depts = departmentService.findAll();
        req.getSession().setAttribute("depts",depts);

        // 查询时数据的回显
        req.setAttribute("param",param);

        req.getRequestDispatcher("/WEB-INF/pages/listemployee.jsp").forward(req,resp);
    }
}
